package com.ghxxzx.dagl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ghxxzx.dagl.entity.Menue;
import com.ghxxzx.dagl.entity.RoleMenue;
import com.ghxxzx.dagl.entity.User;
import com.ghxxzx.dagl.mapper.MenueMapper;
import com.ghxxzx.dagl.mapper.RoleMenueMapper;
import com.ghxxzx.dagl.mapper.UserMapper;
import com.ghxxzx.dagl.vo.MenueVo;
import com.ghxxzx.dagl.vo.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * desc:
 *
 * @author cx316
 * Created by cx316 on 2020-05-29.
 */
@RestController
public class LoginController {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    UserMapper userMapper;
    @Autowired
    MenueMapper menueMapper;
    @Autowired
    RoleMenueMapper roleMenueMapper;
    @RequestMapping("doLogin")
    public ResultEntity login(User user, HttpSession session){
        User user2 = userMapper.selectByUserNameAndPassword(user.getAccount(),user.getPassword());
        if(user2 != null){
            session.setAttribute("user",user2);
            session.setMaxInactiveInterval(7*24*60*60);
            return ResultEntity.ok();
        }
        return ResultEntity.error("账户或密码错误");
    }
    @RequestMapping("zxLogin")
    public ResultEntity zxLogin(HttpSession session) {
        session.invalidate();
        return ResultEntity.ok();
    }

    @RequestMapping("qx")
    public ResultEntity qx(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<RoleMenue> roleMenues = roleMenueMapper.selectByRoleId(user.getRoleId());
        List<Integer> collect = roleMenues.stream().map(RoleMenue::getMenueId).collect(Collectors.toList());
        QueryWrapper<Menue> wrapper = new QueryWrapper();
        wrapper.in("id",collect);
        List<Menue> menues = menueMapper.selectList(wrapper);
        List<MenueVo> menueVoList = new ArrayList<>();

        for(Menue menue : menues){
            Integer parentId = menue.getParentId();
            if(parentId==0){
                MenueVo menueVo = new MenueVo();
                menueVo.setId(menue.getId2());
                menueVo.setText(menue.getName());
                menueVo.setTempId(menue.getId());
                menueVo.setIcon(menue.getIcon());
                menueVo.setUrl(menue.getUrl());
                menueVo.setTargetType(menue.getTargetType());
                menueVoList.add(menueVo);
            }
        }
        ArrayList<MenueVo> menueVos = new ArrayList<>();
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("档案录入")){
                menueVos.add(menueVo);
            }
        }
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("档案接收")){
                menueVos.add(menueVo);
            }
        }
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("档案借阅")){
                menueVos.add(menueVo);
            }
        }
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("档案查询")){
                menueVos.add(menueVo);
            }
        }
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("档案统计")){
                menueVos.add(menueVo);
            }
        }
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("档案依据")){
                menueVos.add(menueVo);
            }
        }
        for (MenueVo menueVo : menueVoList) {
            if (menueVo.getText().equalsIgnoreCase("系统管理")){
                menueVos.add(menueVo);
            }
        }


        for(MenueVo menueVo : menueVos){
            List<MenueVo> children = new ArrayList<>();
            for(Menue menue : menues){
                Integer parentId = menue.getParentId();
                if(parentId == menueVo.getTempId()){
                    MenueVo menueVo1 = new MenueVo();
                    menueVo1.setId(menue.getId2());
                    menueVo1.setText(menue.getName());
                    menueVo1.setTempId(menue.getId());
                    menueVo1.setIcon(menue.getIcon());
                    menueVo1.setUrl(menue.getUrl());
                    menueVo1.setTargetType(menue.getTargetType());
                    children.add(menueVo1);
                }
            }
            menueVo.setChildren(children);
        }
        return ResultEntity.ok(menueVos);
    }
    @RequestMapping("who")
    public ResultEntity who(HttpSession session){
        Object user = session.getAttribute("user");
        return ResultEntity.ok(user);
    }
}
